package com.example.demo.mvvm.anim;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.DecelerateInterpolator;

import java.math.BigDecimal;

/**
 * Created by 80711 on 2018/5/21.
 */

public class CircularRevealAnim {
    private AnimListener mListener;

    public interface AnimListener {
        void onHideAnimationEnd();

        void onShowAnimationEnd();
    }

    private void actionOtherVisible(final boolean isShow, View triggerView, final View animView) {

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            if (isShow) {
                animView.setVisibility(View.VISIBLE);
                if (mListener != null) {
                    mListener.onShowAnimationEnd();
                }
            } else {
                animView.setVisibility(View.GONE);
                if (mListener != null) {
                    mListener.onHideAnimationEnd();
                }
            }
            return;
        }

        int[] tvLocation = new int[2];
        triggerView.getLocationInWindow(tvLocation);
        int tvX = tvLocation[0] + triggerView.getWidth() / 2;
        int tvY = tvLocation[1] + triggerView.getHeight() / 2;

        int[] avLocation = new int[2];
        animView.getLocationInWindow(avLocation);
        int avX = avLocation[0] + animView.getWidth() / 2;
        int avY = avLocation[1] + animView.getHeight() / 2;

        int rippleW = tvX < avX ? animView.getWidth() - tvX : tvX - avLocation[0];

        int rippleH = tvY < avY ? animView.getHeight() - tvY : tvY - avLocation[1];

        double maxRadius = Math.sqrt((rippleW * rippleW + rippleH * rippleH));

        double startRadius;
        double endRadius;

        if (isShow) {
            startRadius = 0d;
            endRadius = maxRadius;
        } else {
            startRadius = maxRadius;
            endRadius = 0d;
        }

        Animator anim = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            anim = ViewAnimationUtils.createCircularReveal(animView, tvX, tvY, BigDecimal.valueOf(startRadius).floatValue(), BigDecimal.valueOf(endRadius).floatValue());
            animView.setVisibility(View.VISIBLE);
            anim.setDuration(500);
            anim.setInterpolator(new DecelerateInterpolator());
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    if (isShow) {
                        animView.setVisibility(View.VISIBLE);
                        if (mListener != null) mListener.onShowAnimationEnd();
                    } else {
                        animView.setVisibility(View.GONE);
                        if (mListener != null) mListener.onHideAnimationEnd();
                    }
                }


            });

            anim.start();
        }

    }


    public void show(View triggerView  ,View showView) {
        actionOtherVisible(true, triggerView, showView);
    }

    public void hide(View triggerView , View hideView ) {
        actionOtherVisible(false, triggerView, hideView);
    }

    public void setAnimListener(AnimListener listener ) {
        mListener = listener;
    }


}
